<template>
  <div>
  <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
    <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
    <el-breadcrumb-item>添加类别</el-breadcrumb-item>
  </el-breadcrumb>
    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item style="margin-left: 11px" label="父级类别" prop="parentId">
        <el-input v-model="ruleForm.parentId"></el-input>
      </el-form-item>
      <el-form-item style="margin-left: 11px" label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item style="margin-left: 11px" label="关键词列表" >
        <el-input v-model="ruleForm.keywords"></el-input>
      </el-form-item>
      <el-form-item style="margin-left: 11px" label="图标">
        <el-input v-model="ruleForm.icon"></el-input>
      </el-form-item>
      <el-form-item style="margin-left: 11px" label="排序序号">
        <el-input v-model="ruleForm.sort"></el-input>
      </el-form-item>
      <el-switch
          style="margin: 10px 0 32px 11px;"
          v-model="ruleForm.value1"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-text=""
          inactive-text="是否启用">
      </el-switch><br>
      <el-switch
          style="margin: 10px 0 32px 11px;"
          v-model="ruleForm.value2"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-text=""
          inactive-text="是否显示在导航栏">
      </el-switch>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
    <el-empty description="当前页面正在开发中,敬请期待......"></el-empty>
  </div>
</template>
<script>
export default {
  data() {
    return {
      ruleForm: {
        parentId: '',
        name: '',
        keywords: '',
        icon: '',
        sort: '',
        value1: true,
        value2: true
      },
      rules: {
        parentId: [
          {required: true, message: '请输入父级类别', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入类别名称', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        pinyin: [
          {required: true, message: '请输入关键词列表', trigger: 'blur'},
          {min: 5, max: 35, message: '长度在 4 到 35 个字符', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '请输入图标', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        keywords: [
          {required: true, message: '请输入排序序号', trigger: 'blur'},
          {min: 5, max: 35, message: '长度在 4 到 35 个字符', trigger: 'blur'}
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/albums/add-new';
          console.log('url = ' + url);
          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);
          this.axios.post(url,formData).then((response) =>{
            let responseBody = response.data;
            console.log('responseBody = ');
            console.log(responseBody);
            if (responseBody.state == 1) {
              this.$message({
                message: responseBody.message,
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
